package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.HistoryMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.History;
import com.aaa.forum.services.IHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class HistoryServiceImpl implements IHistoryService {

    @Resource
    private HistoryMapper historyMapper;

    @Override
    public History selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return historyMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Long userId, Long articleId) {
        // 非空校验
        if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验是否已经存在
        History history = historyMapper.selectByUserIdAndArticleId(userId, articleId);
        int row;
        if (history == null) {
            // 不存在，insert
            history = new History();
            history.setUserId(userId);
            history.setArticleId(articleId);
            history.setState((byte) 1);
            history.setDeleteState((byte) 0);
            Date date = new Date();
            history.setCreateTime(date);
            history.setUpdateTime(date);
            row = historyMapper.insertSelective(history);
        } else {
            // 已经存在，update
            History updateObject = new History();
            updateObject.setId(history.getId());
            updateObject.setState((byte) 1);
            updateObject.setDeleteState((byte) 0);
            updateObject.setUpdateTime(new Date());
            row = historyMapper.updateByPrimaryKeySelective(updateObject);
        }
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void delete(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验是否存在
        History history = historyMapper.selectByPrimaryKey(id);
        if (history == null || history.getDeleteState() == 1) {
            // 记录不存在，无法删除
            log.warn(ResultCode.FAILED_HISTORY_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_HISTORY_NOT_EXISTS));
        }
        // 构造更新对象
        History updateObject = new History();
        updateObject.setId(id);
        updateObject.setState((byte) 0);
        updateObject.setUpdateTime(new Date());
        int row = historyMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public List<History> selectAllByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return historyMapper.selectAllByUserId(userId);
    }

    @Override
    public void deleteByArticleId(Long articleId) {
        if (articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        historyMapper.deleteByArticleId(articleId);
    }
}
